import {
	appID
} from "@/utils/config.js"

function checkEnvironment() {
	let userAgent = navigator.userAgent.toLowerCase();
	let isWeChat = userAgent.indexOf('micromessenger') !== -1;
	let isRegularBrowser = !isWeChat;

	return {
		isWeChat: isWeChat,
		isRegularBrowser: isRegularBrowser
	};
}
/**
 * 统一支付接口
 * @param {Object} orderInfo 订单信息
 * @returns {Promise} 支付结果
 */
export function doPay(orderInfo) {
	return new Promise((resolve, reject) => {
		// #ifdef APP-PLUS
		// App支付逻辑
		uni.requestPayment({
			provider: 'wxpay', // 或 'wxpay'
			orderInfo: {
				appId: appID,
				...orderInfo
			}, // 从服务端获取
			success: function(res) {
				resolve(res);
			},
			fail: function(err) {
				reject(err);
			}
		});
		// #endif

		// #ifdef H5
		// 使用检测结果
		let environment = checkEnvironment();
		if (environment.isWeChat) {
			window.WeixinJSBridge.invoke(
				'getBrandWCPayRequest', {
					...orderInfo,
				},
				function(res) {
					console.log('支付的打印', res);
					// 根据支付结果执行相应的逻辑
					if (res.err_msg === "get_brand_wcpay_request:ok") {
						// 支付成功
						uni.showToast({
							title: '支付成功',
							icon: 'none'
						});
						resolve(200);
					} else if (res.err_msg === "get_brand_wcpay_request:fail") {
						// 支付失败 
						uni.showToast({
							title: '支付失败，请重试',
							icon: 'none'
						});
						resolve(500);
					} else if (res.err_msg === "get_brand_wcpay_request:cancel") {
						// 用户取消支付
						uni.showToast({
							title: '支付已取消',
							icon: 'none'
						});
						resolve(-200);
					} else {
						// 其他错误情况
						resolve(500);
						uni.showToast({
							title: '支付异常',
							icon: 'none'
						});
					}
				}
			);
		} else {
			console.log()
			if (document.addEventListener) {
				document.addEventListener('WeixinJSBridgeReady', () => {
					onBridgeReady(appId, timeStamp, nonceStr, payPackage, signType, paySign);
				}, false);
			} else if (document.attachEvent) {
				document.attachEvent('WeixinJSBridgeReady', () => {
					onBridgeReady(appId, timeStamp, nonceStr, payPackage, signType, paySign);
				});
				document.attachEvent('onWeixinJSBridgeReady', () => {
					onBridgeReady(appId, timeStamp, nonceStr, payPackage, signType, paySign);
				});
			}
		}
		// #endif

		// #ifdef MP-WEIXIN
		// 微信小程序支付
		uni.requestPayment({
			...payInfo,
			success: function(res) {
				resolve(res);
			},
			fail: function(err) {
				reject(err);
			}
		});
		// #endif
	});
}

/**
 * 分享内容
 * @param {Object} options 分享选项
 */
export function shareContent(options) {
	// #ifdef APP-PLUS
	// App分享逻辑
	uni.share({
		provider: 'weixin',
		scene: 'WXSceneSession',
		type: 0,
		title: options.title,
		summary: options.desc,
		imageUrl: options.image,
		href: options.url,
		success: function() {
			console.log('分享成功');
		}
	});
	// #endif

	// #ifdef H5
	// H5分享逻辑
	if (navigator.share) {
		navigator.share({
			title: options.title,
			text: options.desc,
			url: options.url
		});
	} else {
		// 复制链接
		uni.setClipboardData({
			data: options.url,
			success: function() {
				uni.showToast({
					title: '链接已复制，请粘贴分享'
				});
			}
		});
	}
	// #endif

	// #ifdef MP
	// 小程序分享逻辑
	return {
		title: options.title,
		path: '/pages/index/index',
		imageUrl: options.image
	};
	// #endif
}